package minh.bac.wcbrazil2014.countdown;

import minh.bac.wcbrazil2014.R;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class CountDownFragment extends Fragment {

	TextView d, h, m, s, mTextField, day, hour, minute, second;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.countdown_layout, container,
				false);
		
		mTextField = (TextView) rootView.findViewById(R.id.tv_caption);
		day = (TextView) rootView.findViewById(R.id.tv_days);
		hour = (TextView) rootView.findViewById(R.id.tv_hours);
		minute = (TextView) rootView.findViewById(R.id.tv_minutes);
		second = (TextView) rootView.findViewById(R.id.tv_seconds);
		d = (TextView) rootView.findViewById(R.id.tv_d);
		h = (TextView) rootView.findViewById(R.id.tv_h);
		m = (TextView) rootView.findViewById(R.id.tv_m);
		s = (TextView) rootView.findViewById(R.id.tv_s);

		// Font path
		String fontPath = "fonts/PagodeBold.otf";
		String fontPath2 = "fonts/Pagode.otf";
		// Loading Font Face
		Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), fontPath);
		Typeface tf2 = Typeface.createFromAsset(getActivity().getAssets(), fontPath2);

		// Applying font
		
		mTextField.setTypeface(tf);
		day.setTypeface(tf);
		hour.setTypeface(tf);
		minute.setTypeface(tf);
		second.setTypeface(tf);
		d.setTypeface(tf2);
		h.setTypeface(tf2);
		m.setTypeface(tf2);
		s.setTypeface(tf2);

		getTimer();
		
		return rootView;
	}

	public void getTimer() {

		Time timerSet = new Time();
		timerSet.set(0, 0, 3, 13, 05, 2014);
		timerSet.normalize(true);
		long millis = timerSet.toMillis(true);

		Time timeNow = new Time();
		timeNow.setToNow();
		timeNow.normalize(true);
		long millis2 = timeNow.toMillis(true);

		long millisset = millis - millis2;

		// final int smillis = (int) (millis);
		// final int smillis2 = (int) (millis2);

		new CountDownTimer(millisset, 1000) {

			@Override
			public void onTick(long millisUntilFinished) {
				// TODO Auto-generated method stub
				// int weeks = (int) ((millisUntilFinished / 1000) / 604800);
				int days = (int) ((millisUntilFinished / 1000) / 86400);
				int hours = (int) (((millisUntilFinished / 1000) - (days * 86400)) / 3600);
				int minutes = (int) (((millisUntilFinished / 1000) - ((days * 86400) + (hours * 3600))) / 60);
				int seconds = (int) ((millisUntilFinished / 1000) % 60);
				// int millicn = (int) (millisUntilFinished / 1000);

				day.setText("" + days);
				hour.setText("" + hours);
				minute.setText("" + minutes);
				second.setText("" + seconds);

				// mTextField.setText(smillis + " " + smillis2 + " " + millicn
				// + "Time remaining " + weeks + " weeks :" + days
				// + " days :" + hours + " hours :" + minutes
				// + " minutes :" + seconds + " seconds ");
			}

			@Override
			public void onFinish() {
				// TODO Auto-generated method stub

				mTextField.setText("World Cup 2014!");
			}
		}.start();
	}
}
